<?php
namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Session;
use think\Cache;
use think\Controller;
use think\captcha\Captcha;

class Index extends Common
{
    public function index()
    {
        $url=Request()->url();
        if($url=='/admin'){
            return '暂无此页面';
        }
        if (session('qm_ad_uid')) {
            return $this->fetch();
        } else {
            // echo "<script> location.hash = '/user/login';</script>";
            return $this->fetch();
        }
    }

    public function get_admininfo()
    {
        if (request()->isGet()) {
            $access_token = session('qm_ad_uid');
            $info = Db::name('user')->field('id,name')->where(array("id" => $access_token))->find();
            if ($info) {
                $result['code'] = 0;
                $result['msg'] = '登录成功';
                $result['data']['username'] = $info['name'];
                $result['data']['sex'] = 1;
            } else {
                $result['code'] = 1;
                $result['msg'] = '获取信息失败';
            }
        } else {
            $result['code'] = 1;
            $result['msg'] = '获取信息失败';
        }
        echo json_encode($result);

    }

    //登录
    public function login()
    {
        if (request()->isPost()) {
            $username = input('post.username');
            $password = input('post.password');
            $vercode = input('post.vercode');
            if (!captcha_check($vercode)) {
                $result['code'] = 1;
                $result['msg'] = '验证码不正确';
                //  $result['data']['access_token']='';
                //  $this->error('验证码不正确');exit();
            } else {
                $re = Db::name('user')->field('status,id,pwd,type')->where(array('phone' => $username))->find();
                if ($re && $re['status'] == 1 && $re['type'] == 2 && password_verify($password,$re['pwd'])) {
                    $result['code'] = 0;
                    $result['msg'] = '登入成功';
                    $result['data']['access_token'] = $re['id'];
                    Db::name('user')->where(array('id' => $re['id']))->update(['login_time' => time()]);
                    cookie('qm_ad_uid', $re['id']);
                    session('qm_ad_uid', $re['id']);
                } elseif ($re && $re['type'] == 1) {
                    $result['code'] = 1;
                    $result['msg'] = '您的账号暂无权限';
                }  elseif ($re && $re['status'] == 2) {
                    $result['code'] = 1;
                    $result['msg'] = '您的账号已冻结，无法登录';
                } else {
                    $result['code'] = 1;
                    $result['msg'] = '账号密码错误';
                }
            }
            echo json_encode($result);

            //echo "<pre>";print_r($_GET);exit();
        } else {
            return $this->fetch();
        }
    }

    //退出登陆
    public function logout()
    {
        if (request()->isPost()) {
            session('qm_ad_uid', null);
            cookie('qm_ad_uid', null);
            $result['code'] = 0;
            $result['msg'] = '退出登录成功';
        } else {
            $result['code'] = 1;
            $result['msg'] = '退出登录失败';
        }
        echo json_encode($result);
    }
    //验证码
    public function verify()
    {
        $config = [
            // 验证码字体大小
            'fontSize' => 30,
            // 验证码位数
            'length' => 4,
            // 关闭验证码杂点
            'useNoise' => true,
        ];

        $captcha = new Captcha($config);
        $captcha->codeSet = '0123456789';
        return $captcha->entry();
    }
    public function no_url(){
        return '暂无此页面';
    }
}
